<template>
  <div>
    <transition name="fadeIn">
      <div id="animateBox" v-show="showImg">
        <div id="break"></div>
        <div id="InBox"></div>
      </div>
    </transition>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from "vue";
const timer1 = ref<number>();
const timer2 = ref<number>();
const showImg = ref<boolean>(false);

onMounted(() => {
  const imgUrl1 =
    "https://ttzxh.icu/api/static/showPic/6076579e1cb5e2fd2548ca1ec994ff0d.jpeg";
  const imgUrl2 =
    "https://ttzxh.icu/api/static/showPic/de64e72e308fb961948ce6d4af4555a2.jpg";
  const imgUrl3 =
    "https://ttzxh.icu/api/static/showPic/4f464b60441b4d2aee82595cb98a2205.jpg";
  const imgUrl4 =
    "	https://ttzxh.icu/api/static/showPic/0768a532afebf96cdae574f5d58d77b3.jpg";
  const imgUrl5 =
    "	https://ttzxh.icu/api/static/showPic/facb301c8c41b70085ae75d3c4021918.jpg";
  const imgUrl6 =
    "https://ttzxh.icu/api/static/showPic/73781e2b3526b9efc048b41a4f581894.jpeg";
  const imgUrl7 =
    "	https://ttzxh.icu/api/static/showPic/c27180e7e444c0ebaa6c84c433be36fb.jpeg";
  let imageSrcArr = [imgUrl1, imgUrl2, imgUrl3, imgUrl4, imgUrl5, imgUrl6, imgUrl7];
  let imgloadPromiseArr: Array<Promise<HTMLImageElement>> = [];
  let imageArr: Array<string> = [];
  for (let i = 0; i < imageSrcArr.length; i++) {
    imgloadPromiseArr[i] = new Promise((resolve, reject) => {
      let img = new Image();
      img.src = imageSrcArr[i];
      img.onload = () => {
        resolve(img);
      };
    });
  }
  imgloadPromiseArr.forEach((item) => {
    item.then((res) => {
      imageArr.push(`url(${(res as HTMLImageElement).currentSrc})`);
      index = imageArr.length;
    });
  });
  showImg.value = true;
  let repeat = true;
  function changeImage(InBox: HTMLDivElement) {
    if (repeat) {
      breakBox.style.zIndex = "-10";
      count++;
      count = count === index ? 0 : count;
      repeat = false;
      setTimeout(() => {
        repeat = true;
        breakBox.style.zIndex = "100";
        let currentImageLength = count === index - 1 ? 0 : count + 1;
        InBox.style.backgroundImage = imageArr[currentImageLength];
      }, 1000);
    }
  }
  let count = 0;
  let index = 0;
  let breakBox: HTMLDivElement = document.querySelector("#break")!;
  let InBox: HTMLDivElement = document.querySelector("#InBox")!;
  InBox.style.backgroundImage = imageArr[1];
  const appendDom = (dom: HTMLElement) => {
    for (let i = 0; i < 100; i++) {
      let div = document.createElement("div");
      let div1 = document.createElement("div");
      div.style.width = "77px";
      div.style.height = "41px";
      div1.style.width = "76px";
      div1.style.height = "40px";
      div1.style.overflow = "hidden";
      div.style.boxSizing = "border-box";
      div.style.backgroundImage = imageArr[0];
      let positionX = -(i % 10) * 76 + "px";
      let positionY = -Math.floor(i / 10) * 40 + "px";
      div.style.backgroundPosition = positionX + " " + positionY;
      div.style.backgroundSize = "760px 400px";
      let style = document.styleSheets[0];
      style.insertRule(`@keyframes secondrotate${i}
        {
            0%,30%{
                transform:scale(1)
            }
            70%
            {transform: rotateX(${180 + Math.random() * 720}deg) rotateY(${
        180 + Math.random() * 720
      }deg)}
            100%
            {transform: rotateX(${180 + Math.random() * 720}deg) rotateY(${
        180 + Math.random() * 720
      }deg)}
        }`);
      style.insertRule(`@keyframes secondrotateS${i}
        {
            0%,32%{
                transform:scale(1);opacity:1;
            }70%
            {transform: translateZ(${300 + Math.random() * 1500}px) translate(${
        (0.5 - Math.random()) * 500
      }px,${(0.5 - Math.random()) * 500}px);opacity:0}
            100%
            {transform: translateZ(${300 + Math.random() * 1500}px) translate(${
        (0.5 - Math.random()) * 500
      }px,${(0.5 - Math.random()) * 500}px);opacity:0}

        }`);
      div1.style.animation = `secondrotateS${i} 4.5s ease-out infinite`;
      div.style.animation = `secondrotate${i} 4.5s ease-out infinite`;
      div.style.transformOrigin = `center center`;
      div.addEventListener("animationstart", () => {
        timer1.value = setTimeout(() => {
          changeImage(InBox);
          div.style.backgroundImage = imageArr[count];
        }, 4000);
      });
      div.addEventListener("animationiteration", () => {
        timer2.value = setTimeout(() => {
          changeImage(InBox);
          div.style.backgroundImage = imageArr[count];
        }, 4000);
      });
      div1.appendChild(div);
      dom.appendChild(div1);
    }
  };
  appendDom(breakBox);
});
onUnmounted(() => {
  typeof timer1 === "number" && clearTimeout(timer1);
  typeof timer2 === "number" && clearTimeout(timer2);
});
</script>

<style scoped lang="scss">
@import url("../../css/comment/animate.css");
#animateBox {
  width: 100vw;
  height: calc(100vh - 50px);
  font-size: 0px;
  //  background-color: rgba(255, 255, 255, 0.6);
  #break {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 760px;
    height: 400px;
    display: flex;
    perspective: 1000px;
    transform-style: preserve-3d;
    flex-wrap: wrap;
    z-index: 100;
  }
  #InBox {
    position: absolute;
    left: 0;
    top: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 760px;
    height: 400px;
    display: flex;
    perspective: 1000px;
    transform-style: preserve-3d;
    flex-wrap: wrap;
    z-index: 10;
    background-size: 760px 400px;
  }
}
</style>
